package co.th.helper.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;

import co.th.helper.common.CommonUtils;
import co.th.helper.jdbc.JdbcTemplate;
import co.th.helper.jdbc.mapper.ImpRowMapper;
import co.th.helper.model.ImpRating;

public class ImpDAO extends JdbcTemplate<co.th.helper.model.ImpRating>{

	private static Logger _logger = Logger.getLogger(ImpDAO.class);
	
	public List<ImpRating> findAll() throws Exception{
		try {
			return findByCriteria(null);
		} catch (Exception ex) {
			_logger.error("Exception : " + ex.getMessage());
			throw new RuntimeException("Can't get all Imp Rating");
		}
	}
	
	public ImpRating findByPK(BigDecimal impCode) throws Exception{
		try {
			ImpRating imp = new ImpRating();
			imp.setImpCode(impCode);
			List<ImpRating> list = findByCriteria(imp);
			if (null == list || list.isEmpty()){
				throw new Exception("Record not found.");
			}
			return list.get(0);
		} catch (Exception ex) {
			_logger.error("Exception : " + ex.getMessage());
			throw new RuntimeException("Cat not get Imp Rating");
		}
	}
	
	public List<ImpRating> findByCriteria(ImpRating imp) throws Exception{
		try {
			StringBuffer sql = new StringBuffer("select * from imp_rating where 1=1 ");
			List<Object> paramObj = new ArrayList<Object>(0);
			
			if (null != imp){
				int index = 0;
				if (null != imp.getImpCode() && imp.getImpCode().intValue() > 0){
					sql.append(" AND imp_code = ? ");
					paramObj.add(index++, imp.getImpCode());
				}
				if (!CommonUtils.chkIsnull(imp.getImpNameTH())){
					sql.append(" AND imp_name_th = ? ");
					paramObj.add(index++, imp.getImpNameTH());
				}
				if (!CommonUtils.chkIsnull(imp.getImpNameEN())){
					sql.append(" AND imp_name_en = ? ");
					paramObj.add(index++, imp.getImpNameEN());
				}
			}
			return this.queryList(sql.toString(), CommonUtils.convListToArray(paramObj), new ImpRowMapper());
		} catch (Exception ex) {
			_logger.error("Exception : " + ex.getMessage());
			throw new RuntimeException("Can't get Imp Rating");
		}
	}
	
	public synchronized ImpRating insert(ImpRating imp) throws Exception{
		try {
			imp.setImpCode(generatePK());
			
			final String sql = "INSERT INTO imp_rating (imp_code, imp_name_th, imp_name_en, create_by, create_date, update_by, update_date) VALUES ( ?, ?, ?, ?, ?, ?, ?)";
			Object[] params = new Object[7];
			params[0] = imp.getImpCode();
			params[1] = imp.getImpNameTH();
			params[2] = imp.getImpNameEN();
			params[3] = imp.getCreateBy();
			params[4] = imp.getCreateDate();
			params[5] = imp.getUpdateBy();
			params[6] = imp.getUpdateDate();
			
			int rowOfProcess = this.saveUnique(sql, params);
			if (rowOfProcess < 1){
				throw new Exception("Can't save Imp Rating data.");
			}
			return imp;
		} catch (Exception ex) {
			_logger.error("Exception : " + ex.getMessage());
			throw ex;
		}
	}
	
	public int update(ImpRating imp) throws Exception{
		try {
			StringBuffer sql = new StringBuffer("UPDATE imp_rating SET ");
			List<Object> paramObj = new ArrayList<Object>(0);
			
			if (imp.getImpCode() == null || imp.getImpCode().intValue() <= 0){
				throw new Exception("No primary key.");
			}
			
			int index = 0;
			sql.append(" update_date = ?  ");
			paramObj.add(index++, new java.sql.Date(Calendar.getInstance().getTime().getTime()));
			if (!CommonUtils.chkIsnull(imp.getImpNameTH())){
				sql.append(" , imp_name_th = ? ");
				paramObj.add(index++, imp.getImpNameTH());
			}
			if (!CommonUtils.chkIsnull(imp.getImpNameEN())){
				sql.append(" , imp_name_en = ? ");
				paramObj.add(index++, imp.getImpNameEN());
			}
			
			sql.append(" WHERE imp_code = ? ");
			paramObj.add(index++, imp.getImpCode());
			
			return updateUnique(sql.toString(), CommonUtils.convListToArray(paramObj), new ImpRowMapper());
		} catch (Exception ex) {
			_logger.error("Exception : " + ex.getMessage());
			throw new RuntimeException("Can't update Imp Rating Data.");
		}
	}
	
	public int delete(BigDecimal impCode) throws Exception{
		try {		
			if (impCode == null || impCode.intValue() <= 0){
				throw new Exception("No primary key (Imp Rating Code).");
			}
			
			final String sql = "DELETE FROM imp_rating WHERE imp_code = ?";
			Object[] param = new Object[]{impCode};
			
			return updateUnique(sql.toString(), param, new ImpRowMapper());
		} catch (Exception ex) {
			_logger.error("Exception : " + ex.getMessage());
			throw new RuntimeException("Can't delete Imp Rating Data.");
		}
	}
	
	private BigDecimal generatePK() throws Exception{
		final String sql = "select max(imp_code) AS imp_code from imp_rating";
		_logger.debug("SQL : " + sql);
		ImpRating objRow = queryUnique(sql, new Object[0], new ImpRowMapper());
		return BigDecimal.valueOf(objRow.getImpCode().intValue() + 1);
	}
}
